package com.xgry.bpm.api.business.function.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xgry.bpm.api.business.author.entity.TBAuthor;
import com.xgry.bpm.api.business.author.service.TBAuthorService;
import com.xgry.bpm.api.business.function.entity.TBFunction;
import com.xgry.bpm.api.business.function.form.TBFunctionForm;
import com.xgry.bpm.api.business.function.service.TBFunctionService;
import com.xgry.bpm.api.business.function.vo.TBFunctionVO;
import com.xgry.bpm.common.utils.EasyPoiUtils;
import com.xgry.bpm.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

;

/**
 * <p>
 * TBFunction前端控制器
 *
 * </p>
 *
 * @author admin
 * @since 2020-07-14
 */
@RestController
@Api(tags = "TBFunction")
@RequestMapping("/tBFunction")
public class TBFunctionController {

    @Autowired
    public TBFunctionService tBFunctionService;

    @Autowired
    public TBAuthorService tbAuthorService;

    @Autowired
    public TBFunctionService tbFunctionService;

    /**
     * 保存单条
     *
     * @param param 保存参数
     * @return 是否添加成功
     */
    @ApiOperation(value = "保存", notes = "保存数据到TBFunction")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "Id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_by", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_name", value = "按钮名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_url", value = "按钮url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_icon", value = "按钮图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_menu_code", value = "所属菜单 FK", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_init", value = "是否预置 0：是1：否", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_code", value = "按钮编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_modular_code", value = "模块编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "i_function_sort", value = "按钮排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_menu_p_name", value = "", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_id", value = "所属模块", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_p_name", value = "父模块", required = false, dataType = ""),
    })
    @RequestMapping(value = "/add.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result addTBFunction(@RequestBody TBFunctionForm param) {

        TBFunction byFunctionName = tBFunctionService.findByFunctionName(param.getVFunctionName());
        if (null != byFunctionName) {
            Result.fail("按钮名称已存在，请重新输入");
        }

        TBFunction byFunctionCode = tBFunctionService.findByFunctionCode(param.getVFunctionCode());
        if (null != byFunctionCode) {
            Result.fail("按钮编码已存在，请重新输入");
        }

        if (1 != tBFunctionService.save(param)) {
            return Result.fail("保存失败");
        }
        return Result.success();
    }


    /**
     * 获取可用按钮列表
     *
     * @param // 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "获取可用按钮列表", notes = "获取可用按钮列表")
    @GetMapping(value = "/findByStatus.do")
    public Result findByStatus() {
        QueryWrapper<TBFunction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("v_function_status",1);
        return Result.success("操作成功", tBFunctionService.list(queryWrapper));
    }

    /**
     * 检查按钮名称是否存在
     *
     * @param // 按钮名称
     * @return 查询结果
     */
    @ApiOperation(value = "检查按钮名称是否存在", notes = "获取可用按钮列表")
    @ApiImplicitParam(paramType = "query", name = "functionName", value = "按钮名称", required = false, dataType = "String")
    @GetMapping(value = "/findByFunctionName.do")
    public Result findByFunctionName(@RequestParam("functionName")String functionName) {

        TBFunction byFunctionName = tBFunctionService.findByFunctionName(functionName);
        if (null != byFunctionName) {
            return Result.fail("按钮编号已存在，请重新输入");
        }
        return Result.success();
    }



    /**
     * 检查按钮名称是否存在
     *
     * @param // 按钮名称
     * @return 查询结果
     */
    @ApiOperation(value = "查询当前角色id下所有按钮", notes = "获取可用按钮列表")
    @ApiImplicitParam(paramType = "query", name = "functionName", value = "按钮名称", required = false, dataType = "String")
    @GetMapping(value = "/findByMenuIdAndUserInfo")
    public Result findByMenuIdAndUserInfo(@RequestParam("roleId")int roleId) {

        List<TBAuthor> authors = tbAuthorService.list(new QueryWrapper<TBAuthor>() {{
            this.eq("roleId", roleId);
        }});

        List<String> buttonList = new ArrayList<>();
        for (TBAuthor author : authors) {
            buttonList.addAll(Arrays.asList(author.getFunctions().split(",")));
        }
        buttonList.stream().distinct().collect(Collectors.toList());

        return Result.success(tbFunctionService.list(new QueryWrapper<TBFunction>() {{
            this.in("v_function_code", buttonList);
        }}));
    }

    public static void main(String[] args) {
        List<String> strings = Arrays.asList("1,2,3".split(","));
        System.out.println(strings);
    }

    /**
     * 按钮排序上移
     *
     * @param // 按钮编号
     * @return 查询结果
     */
    @ApiOperation(value = "按钮上移", notes = "按钮上移")
    @ApiImplicitParam(paramType = "query", name = "id", value = "按钮id", required = false, dataType = "int")
    @GetMapping(value = "/lastButtons.do")
    public Result lastButtons(@RequestParam("id")Integer id) {
        TBFunction tb= tBFunctionService.lastButtons(id);
        if (null == tb) {
            return Result.fail("当前按钮没有上一层数据");
        }
        return Result.fail();
    }


    /**
     * 按钮排序下移
     *
     * @param // 按钮编号
     * @return 查询结果
     */
    @ApiOperation(value = "按钮下移", notes = "按钮下移")
    @ApiImplicitParam(paramType = "query", name = "id", value = "按钮id", required = false, dataType = "int")
    @GetMapping(value = "/nextButtons.do")
    public Result nextButtons(@RequestParam("id") Integer id) {
        TBFunction tbFunction = tBFunctionService.nextButtons(id);
        if (null == tbFunction) {
            return Result.fail("当前按钮没有下一层数据");
        }
        return Result.success();
    }







    /**
     * 更新(根据主键id更新)
     *
     * @param param 修改参数
     * @return 是否更改成功
     */
    @ApiOperation(value = "更新数据", notes = "根据主键id更新TBFunction数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "Id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_by", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_name", value = "按钮名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_url", value = "按钮url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_icon", value = "按钮图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_menu_code", value = "所属菜单 FK", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_init", value = "是否预置 0：是1：否", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_code", value = "按钮编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_modular_code", value = "模块编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "i_function_sort", value = "按钮排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_menu_p_name", value = "", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_id", value = "所属模块", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_p_name", value = "父模块", required = false, dataType = ""),
    })
    @RequestMapping(value = "/updateById.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result updateTBFunctionById(@RequestBody TBFunctionForm param) {
        if (1 != tBFunctionService.updateById(param)) {
            return Result.fail("保存失败");
        }
        return Result.success();
    }


    /**
     * 删除(根据主键id删除)
     *
     * @param //主键id
     * @return 是否删除成功
     */
    @ApiOperation(value = "删除数据", notes = "根据主键id删除TBFunction数据")
    @ApiImplicitParam(paramType = "query", name = "", value = "Id", required = true, dataType = "String")
    @RequestMapping(value = "/deleteById.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @Transactional
    public Result deleteTBFunctionById(String[] ids) {
        int result = 0;
        for (String id : ids) {
            tBFunctionService.deleteById(id);
            result++;
        }

        if (result > 0) {
            return Result.fail("删除失败！");
        }

        return Result.success("操作成功");
    }

    /**
     * 根据主键id查询单条
     *
     * @param // 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "获取单条数据", notes = "根据主键id获取TBFunction数据")
    @ApiImplicitParam(paramType = "query", name = "", value = "Id", required = true, dataType = "String")
    @RequestMapping(value = "/getById.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result getTBFunctionById(@RequestParam(value = "Id") String id) {
        TBFunctionVO result = tBFunctionService.selectById(id);
        if (null == result) {
            return Result.fail("无数据！");
        }
        return Result.success("操作成功", result);
    }

    /**
     * 查询全部
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "全部查询", notes = "查询TBFunction全部数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "Id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_by", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_name", value = "按钮名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_url", value = "按钮url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_icon", value = "按钮图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_menu_code", value = "所属菜单 FK", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_init", value = "是否预置 0：是1：否", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_code", value = "按钮编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_modular_code", value = "模块编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "i_function_sort", value = "按钮排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_menu_p_name", value = "", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_id", value = "所属模块", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_p_name", value = "父模块", required = false, dataType = ""),
    })
    @RequestMapping(value = "/queryAll.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result getTBFunctionAll(@RequestBody TBFunctionForm param) {
        List<TBFunctionVO> result = tBFunctionService.selectAll(param);
        if (CollectionUtils.isEmpty(result)) {
            return Result.fail("无数据！");
        }
        return Result.success("操作成功", result);
    }




    /**
     * 分页查询
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询", notes = "分页查询TBFunction全部数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "Id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_by", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_name", value = "按钮名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_url", value = "按钮url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_icon", value = "按钮图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_menu_code", value = "所属菜单 FK", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_init", value = "是否预置 0：是1：否", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_function_code", value = "按钮编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_modular_code", value = "模块编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "i_function_sort", value = "按钮排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_menu_p_name", value = "", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_id", value = "所属模块", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_p_name", value = "父模块", required = false, dataType = ""),
    })
    @RequestMapping(value = "/queryPage.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result getTBFunctionPage(@RequestBody TBFunctionForm param) {
        IPage<TBFunctionVO> result = tBFunctionService.selectPage(param);
        if (CollectionUtils.isEmpty(result.getRecords())) {
            return Result.fail("无数据！");
        }
        return Result.convertIpageToSuccess(result);
    }

    /**
     * Excel导入
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "Excel导入", notes = "导入全字段")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "", value = "", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "", value = "", required = false, dataType = "String")
    })
    @RequestMapping(value = "/importExcel.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result importExcel(@RequestParam MultipartFile file) throws Exception {
        List<TBFunction> list = EasyPoiUtils.importExcel(file, 0, 1, TBFunction.class);
        if (CollectionUtils.isEmpty(list)) {
            return Result.fail("无数据");
        }
        return Result.success();
    }

    /**
     * Excel导出
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "Excel导出", notes = "导出全字段")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "current", value = "页码", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "size", value = "每页条数", required = false, dataType = "String")

    })
    @RequestMapping(value = "/exportExcel.do", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public void exportExcel(HttpServletResponse response) throws Exception {
        List<TBFunction> result = tBFunctionService.list();
        EasyPoiUtils.exportExcel(result, "按钮表列表", "按钮表报表", TBFunction.class, "按钮表明细报表.xls", response);

    }


}

